<template>
  <div>
    <div class="card" style="margin-bottom: 10px;">
      <el-input v-model="data.categoryName" :prefix-icon="Search" style="width: 300px; margin-right: 10px"
        placeholder="请输入餐饮类目名称查询"></el-input>
      <el-button type="primary" style="margin-left: 10px;" @click="load">查询</el-button>
      <el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
    </div>
    <div class="card" style="margin-bottom: 10px">
      <div style="margin-bottom: 10px">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-table stripe :data="data.tableData">
        <el-table-column label="类目编号" prop="categoryId"></el-table-column>
        <el-table-column label="类目名称" prop="categoryName"></el-table-column>
        <el-table-column label="类目类型" prop="categoryType"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
        <el-table-column label="修改时间" prop="updateTime"></el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template v-slot="scope">
            <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" @click="handleDel(scope.row.categoryId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="card">
      <el-pagination background layout="prev, pager, next" @current-change="handelCurrentChange"
        v-model:page-size="data.pageSize" v-model:current-page="data.pageNum" v-model:total="data.total" />
    </div>

    <el-dialog title="信息" width="40%" v-model="data.formVisible" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="data.form" label-width="100px" style="padding-right: 50px">
        <el-form-item label="类目名称" prop="categoryName">
          <el-input v-model="data.form.categoryName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类目类型" prop="categoryType">
          <el-input v-model="data.form.categoryType" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.formVisible = false">取 消</el-button>
          <el-button type="primary" @click="save()">保 存</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import request from "@/utils/request";
import { reactive } from "vue";
import { ElMessageBox } from "element-plus";



const data = reactive({
  pageNum: 1,
  pageSize: 8,
  total: 0,
  formVisible: false,
  categoryName: '',
  form: {},
  tableData: [
    { categoryId: '1', categoryName: '冰淇淋', categoryType: '1', createTime: '2023-10-01', updateTime: '2023-10-02' },
    { categoryId: '2', categoryName: '冰淇淋', categoryType: '1', createTime: '2023-10-01', updateTime: '2023-10-02' },
  ]
})

const load = () => {
  request.get("/seller/productCategory/page", {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      categoryName: data.categoryName,
    }
  }).then(res => {
    data.tableData = res.data?.list || [];
    data.total = res.data?.total || 0;
  })
}

const save = () => {
  request.request({
    url: data.form.categoryId ? '/seller/productCategory/update' : '/seller/productCategory/add',
    method: data.form.categoryId ? 'put' : 'post',
    data: data.form
  }).then(res => {
    if (res.code === '200') {
      load();
      data.formVisible = false;
      ElMessage.success(res.msg || '操作成功');
    } else {
      ElMessage.error(res.msg || '操作失败');
    }
  })
}



const handleAdd = () => {
  data.form = {}
  data.formVisible = true
}
const handleEdit = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}
//点击删除
const handleDel = (categoryId) => {
  ElMessageBox.confirm('删除数据后无法恢复，您确认删除吗？', '删除确认', { type: 'warning' }).then(res => {
    request.delete('/seller/productCategory/delete/' + categoryId).then(res => {
      if (res.code === '200') {
        load()    // 重新获取数据
        ElMessage.success(res.msg || '删除成功')
      } else {
        ElMessage.error(res.msg || '删除失败')
      }
    })
  }).catch(res => {
  })
}

//翻页
const handelCurrentChange = (pageNum) => {
  // 当翻页的时候重新加载数据即可
  load()
}

//重置
const reset = () => {
  data.categoryName = ''
  load()
}



load();
</script>